﻿using System;
using System.IO;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using ASPNETVN.PORTAL.Components.Helpers;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Controls
{
    public partial class AddEditDocument : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Item document = Item.Load(this.ItemID);
                    if (document != null)
                    {
                        txtCategory.Text = document.Category;
                        txtTitle.Text = document.Title;
                        btnUpload.Enabled = true;
                    }
                }
                else
                {
                    btnUpload.Enabled = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                // Lưu thông tin vào CSDL.
                Item document = new Item();
                document.ModuleID = this.ModuleID;
                document.Category = this.txtCategory.Text;
                document.Title = this.txtTitle.Text;

                document.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                document.CreatedDate = DateTime.Now;
                if (this.ItemID == 0)
                {
                    this.ItemID = document.Insert();
                }
                else
                {
                    document.ID = this.ItemID;
                    document.Update();
                }
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, this.ItemID, "page=AddEditDocument");
                Response.Redirect(url);
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, this.ItemID, "page=UploadDocument");
            Response.Redirect(url);
        }
    }
}